
/**
 * @name Ccay.task.Import2
 * @description Classify 视图页面控制器
 * @author chaowangbang
 * @since 2011-11-29
 * @param page
 *            该参数为控制器对象的内部别名，方便控制器内部的调用。外部访问不到，外部只能通过Ccay.lookup.ListClassify访问其方法或属性
 */

Ccay.Module.define("Ccay.task.Import2", function(page, $S) {
			page.ready = function() {
				$("#ccay-operate").ccayToolbar({
							items : [{
								type : "imgBtn",
								icon : "search",
								text : "ccay.common.button.search11",
								css : "default", // 回车搜索的按钮的class
								attr : {
									id : "btnSearch"
								}
							}, {
								type : "imgBtn",
								icon : "reset",
								text : "ccay.common.button.reset",
								css : "reset",
								attr : {
									id : "reset"
								}
							}]
						});
				$("#ccay-grid-toolbar").ccayToolbar({
							items : [{
								type : "imgBtn",
								icon : "del",
								text : "ccay.common.button.delete",
								click : Ccay.task.Import2.del,
								attr : {
									id : "del"
								}
							}]
						});
				// consumeTime渲染器参数设置
				var showUnit = {
					// getFormat初始化时，返回设置页面显示格式，如：2s
					getFormat: function (field, format){
						return "${consumeTime}" + "s";
					}
				}
				var gridOptions = {
					//height : 300,
					percentWidth : true,
					wrap : true,
					showSwitch:true,
					allowFilter: true,
					switchMode:"search",
					genSearchByFilter:true,
					minHeight : 24,
					// 设置igrid缓存数据
					cacheData : true,
					// 设置缓存key值
					cacheKey : ["taskId"],
					// 分组显示
					//update 2014.1.1 hufeng 00214522 添加开关
					rowRender : Ccay.UI.Config.excel.rowRender,
					//end update
					rowRenderOp : {
						simpleSort : true,
						spareFields : ["creationDate", "typeId", "importStatus"]
					},
					pagerOp : {
						anchorPanel : "#taskImportGrid"
					},
					showToolBar: true,
					customSetting: true,
					toolPanel: "#ccay-grid-toolbar",
					// 列设置
					columns : [{
						width : "5%",
						columnType : 'select',
						multi : true,
						freezeable : true
					},
					
					{
						field : 'displayFileName',
						header : "ccay.task.label.importFileName",
						cssName : "left",
						filter: true,
						//renderer : Ccay.UI.Renderer.create(fileNameDownLoadRenderer),
						width : "15%",
						content : "${#fileNameDownLoadRenderer(data)}"
					}, {
						field : 'typeId',
						header : "ccay.task.label.importTaskType",
						cssName : "left",
						width : "7%",
						filter: {enumable: true,ajax:{url:"services/ccay/task/import/list"},valueField:"typeId"}
					}, {
						field : 'importStatus',
						header : "ccay.task.label.importStatus",
						cssName : "center",
						width : "8%",
						filter:{input:false,defaultFilters: false,defines:[{value : "0", text:"ccay.common.message.waiting"},{value : "1", text:"ccay.common.message.inProcess"},{value : "2", text:"ccay.common.message.succeed"},{value : "3", text:"ccay.common.message.partialSucceed"},{value : "4", text:"ccay.common.message.failed"}]},
						content : "${#getStatus(data)}",
						getGroupValue : function(value, record) { // 渲染分组操作的组名
							if (value == "0") {
								return $i18n("ccay.common.message.waiting");
							} else if (value == "1") {
								return $i18n("ccay.common.message.inProcess");
							} else if (value == "2") {
								return $i18n("ccay.common.message.succeed");
							} else if (value == "3") {
								return $i18n("ccay.common.message.partialSucceed");
							} else if (value == "4") {
								return $i18n("ccay.common.message.failed");
							}
							return "";
						}
					}, {
						field : 'fileSize',
						hidden : true,
						header : "ccay.task.label.importFileSize",
						cssName : "right",
						content : "${fileSizeRender(data)}",
						width : "10%"
					}, {
						field : 'operateQty',
						hidden : true,
						header : "ccay.task.label.importOperateQty",
						cssName : "right",
						width : "10%",
						filter: true,
						type : "integer"
					}, {
						field : 'importedQty',
						hidden : true,
						header : "ccay.task.label.importedQty",
						cssName : "right",
						width : "10%",
						filter: true,
						type : "integer"
					}, {
						field : 'creationDate',
						group : true,
						width : "10%",
						filter: {range:true},
						type: "date",
						header : "ccay.task.label.importCreatedTime",
						cssName : "center",
						content : "${#thisToDateBefore(data.creationDate)}",
						getGroupValue : function(value, record) {// 渲染分组操作的组名
							return value.toDate().getUTCTime().format();
						}
					}, {
						field : 'runStartTime',
						hidden : true,
						width : "10%",
						filter: {range:true},
						type: "date",
						header : "ccay.task.label.importRunStartTime",
						cssName : "center",
						content : "${#thisToDateBefore(data.runStartTime)}"
					}, {
						field : 'consumeTime',
						hidden : true,
						header : "ccay.task.label.importConsumeTime",
						type : "integer",
						renderer: Ccay.UI.Renderer.create(showUnit),
						cssName : "right",
						width : "10%"
					}],
					// 设置igrid的ajax请求参数，url属性获取数据源的链接地址
					ajax : {
						url : 'services/ccay/task/import/list/page/${pageSize}/${curPage}/${filter}'
					},
					operation : {
						search : {
							// 查询按钮id选择器
							//btn : '#btnSearch',
							// 查询条件区域id选择器
							panel : '#searchForm'
							// 查询请求地址
							//url : 'services/ccay/task/import/list/page/{0}/{1}'
						}

					}
					
				}
			// 初始化igrid
				$S("#taskImportGrid").igrid(gridOptions);
			};
			page.listImportStatus = function() {
				return [{
					value : 0,
					text : $i18n("ccay.common.message.waiting")
				}, {
					value : 1,
					text : $i18n("ccay.common.message.inProcess")
				}, {
					value : 2,
					text : $i18n("ccay.common.message.succeed")
				}, {
					value : 3,
					text : $i18n("ccay.common.message.partialSucceed")
				}, {
					value : 4,
					text : $i18n("ccay.common.message.failed")
				}];
			}

			/**
			 * 清除用户信息
			 */
			page.clearUserId4ldap = function(obj) {
				if (!obj.value) {
					$("input:hidden[name='createdBy']").val("");
				}
			}

			/** grid的delete按钮 */
			page.del = function() {
				var grid = "#taskImportGrid";
				var records = $(grid).manager().getSelectedRecords();
				var url = "services/ccay/task/import/d/list";
				if (records.length > 0) {
					Ccay.UI.confirm("ccay.common.message.confdel", function(result) {
								if (result) {
									Ccay.doPut(url, records, function() {
											Ccay.UI.topBox("ccay.common.message.success");
											// 调用刷新grid的方法
											$(grid).manager().reload(true, true, true);										
											// $("#btnSearch").click();
										});
								}
							});
				} else {
					Ccay.UI.topBox({content:"ccay.common.message.selectedRecords",style:"warn"});
					//Ccay.UI.showMessage("ccay.common.message.selectedRecords", "warn");
				}
			}

			function userId2Account(value, record, columnObj, grid, colNo, rowNo) {
				return "<a title={0} class='init PersonInfo' id='userAccount'  scope='intranet'>{1}</a>".formatValue(value, value);
			}

			// 设置任务状态 （0:waiting ,1:inProcess , 2:succeed ,3:partialSucceed,4: Failed )
			getStatus = function(record) {
				
				var dis = "", msg = "";
				var value = record.importStatus;
				switch (value) {
					case "0" :
						dis = $i18n("ccay.common.message.waiting");
						if (record.remark != null) {
							dis += record.filePath?"<a href='{0}&remark=true' class = 'ccay-icon download' title='{1}' ></a>".formatValue(record.filePath,$i18n("ccay.integration.label.processErrorDownload")):"<a href='servlet/download?dlType=Excel&excelId=" + record.taskId + "&execFlag=IMP&moduleName=" + record.typeId + "&remark=true' class = 'ccay-icon download' title='" + $i18n("ccay.integration.label.processErrorDownload") + "' ></a>";
						}
						break;

					case "1" :
						dis = $i18n("ccay.common.message.inProcess");
						if (record.remark != null) {
							dis += record.filePath?"<a href='{0}&remark=true' class = 'ccay-icon download' title='{1}' ></a>".formatValue(record.filePath,$i18n("ccay.integration.label.processErrorDownload")):"<a href='servlet/download?dlType=Excel&excelId=" + record.taskId + "&execFlag=IMP&moduleName=" + record.typeId + "&remark=true' class = 'ccay-icon download' title='" + $i18n("ccay.integration.label.processErrorDownload") + "' ></a>";
						}
						break;
					case "2" :
						var succeedShow = $i18n("ccay.common.message.succeed");
						var succeedSize = record.fileSize ? Ccay.Format.toFileSize(record.fileSize) : "0";
						var succeedTitle = $i18n("ccay.task.label.importFileSize")+ ":" + succeedSize +"\n"+ $i18n("ccay.task.label.importOperateQty") + ":" + record.operateQty + "\n" + $i18n("ccay.task.label.importedQty") + ":" + record.importedQty;
						if (record.remark != null) {// 此处是为了让下载图标和导入状态的信息显示到一行
							dis = record.filePath?"<div title='{0}' ><font>{1}</font><a href='{2}&remark=true' class = 'ccay-icon download' title='{3}' ></a></div>".formatValue(succeedTitle,succeedShow,record.filePath,$i18n("ccay.integration.label.processErrorDownload")):"<div title='"+succeedTitle+"' ><font>"+succeedShow+"</font><a href='servlet/download?dlType=Excel&excelId="+record.taskId+"&execFlag=IMP&remark=true' class = 'ccay-icon download' title='"+$i18n("ccay.integration.label.processErrorDownload")+"' ></a></div>";
							//dis = "<div title='"+succeedTitle+"' ><font>"+succeedShow+"</font><a href='servlet/download?dlType=Excel&excelId="+record.taskId+"&execFlag=IMP&remark=true' class = 'ccay-icon download' title='"+$i18n("ccay.integration.label.processErrorDownload")+"' ></a></div>";
						} else {
							dis = "<div title='"+succeedTitle+"' ><font>"+succeedShow+"</font></div>";
						}
						break;
					case "3" :
						var succeedShow = $i18n("ccay.common.message.partialSucceed");
						var succeedSize = record.fileSize ? Ccay.Format.toFileSize(record.fileSize) : "0";
						var succeedTitle =$i18n("ccay.integration.label.processErrorDownload")+"\n"+ $i18n("ccay.task.label.importFileSize") + ":" + succeedSize + "\n" + $i18n("ccay.task.label.importOperateQty") + ":" + record.operateQty + "\n" + $i18n("ccay.task.label.importedQty") + ":" + record.importedQty;
						if (record.remark != null) {
							//dis = "<div title='"+succeedTitle+"'><a href='servlet/download?dlType=Excel&excelId="+record.taskId+"&execFlag=IMP&remark=true'><font  color='red' style='text-decoration: underline'>"+succeedShow+"</font></a><a class = 'ccay-icon download' href='servlet/download?dlType=Excel&excelId="+record.taskId+"&execFlag=IMP&remark=true'></a></div>";
							dis = record.filePath?"<div title='{0}'><a href='{1}&remark=true' class='link-special'>{2}</a><a class = 'ccay-icon download' href='{1}&remark=true'></a></div>".formatValue(succeedTitle,record.filePath,succeedShow):"<div title='"+succeedTitle+"'><a href='servlet/download?dlType=Excel&excelId="+record.taskId+"&execFlag=IMP&remark=true' class='link-special'>"+succeedShow+"</a><a class = 'ccay-icon download' href='servlet/download?dlType=Excel&excelId="+record.taskId+"&execFlag=IMP&remark=true'></a></div>";
							//dis = "<div title='"+succeedTitle+"'><a href='servlet/download?dlType=Excel&excelId="+record.taskId+"&execFlag=IMP&remark=true' class='link-special'>"+succeedShow+"</a><a class = 'ccay-icon download' href='servlet/download?dlType=Excel&excelId="+record.taskId+"&execFlag=IMP&remark=true'></a></div>";
						} else {
							dis = "<div title='"+succeedTitle+"' >"+succeedShow+"</div>";
						}
						break;
					case "4" :
						dis = $i18n("ccay.common.message.failed");
						var forId="showFailed"+record.taskId;
						//var url = record.remark ? "<a title='" + $i18n("ccay.integration.label.processErrorDownload") + "' href='servlet/download?dlType=Excel&excelId=" + record.taskId + "&execFlag=IMP&remark=true'><font color='red' style='text-decoration: underline'>"+dis+"</font></a><a class = 'ccay-icon download' href='servlet/download?dlType=Excel&excelId=" + record.taskId + "&execFlag=IMP&remark=true'></a>" : "";
						var url ="";
						if (record.remark) {
							url = record.filePath?"<a title='{0}' href='{1}&remark=true'>{2}</a><a class = 'ccay-icon download' href='{1}&remark=true'></a>".formatValue($i18n("ccay.integration.label.processErrorDownload"),record.filePath,dis) : "<a title='" + $i18n("ccay.integration.label.processErrorDownload") + "' href='servlet/download?dlType=Excel&excelId=" + record.taskId + "&execFlag=IMP&remark=true'>"+dis+"</a><a class = 'ccay-icon download' href='servlet/download?dlType=Excel&excelId=" + record.taskId + "&execFlag=IMP&remark=true'></a>";
						}else{
							url = ""
						}
						//var url = record.remark ? "<a title='" + $i18n("ccay.integration.label.processErrorDownload") + "' href='servlet/download?dlType=Excel&excelId=" + record.taskId + "&execFlag=IMP&remark=true'>"+dis+"</a><a class = 'ccay-icon download' href='servlet/download?dlType=Excel&excelId=" + record.taskId + "&execFlag=IMP&remark=true'></a>" : "";
						dis=record.remark?url:dis;
						break;
					default :
				}
				var msg = record.errorMsg;
				if (msg) {
					return setShowError(msg, dis);
				}
				return dis;
			};

			setShowError = function(msg, dis) {
				msg = msg.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&acute;').replace(/"/g, '&quot;').replace(/(^\s*)|(\s*$)/g, "").replace(/\n/g, "");
				
				return"<div title='" + $i18n("ccay.integration.label.processError") + "' ><a style='cursor:pointer' onclick=\"Ccay.UI.showMessage('" + msg + "','error')\"><font color='red' style='text-decoration: underline'>" + dis + "</font></a><a onclick=\"Ccay.UI.showMessage('" + msg + "','error')\" class='ccay-icon warning'></a></div>";
			};

			// fileSize的规格化
			fileSizeRender = function(data) {
				return Ccay.Format.toFileSize(data.fileSize);
			};

			// 对时间进行格式化处理
			thisToDateBefore = function(data) {
				return Ccay.Format.toDateBefore(data && data.getUTCTime());
			};

			fileNameDownLoadRenderer = function (record) {
				return "<a href='servlet/download?dlType=Excel&excelId={0}&execFlag=IMP' title='{1}' class='link-special'>{2}</a>".formatValue(record.taskId, $i18n("ccay.edm.label.downloadFile"), record.displayFileName);
		   };
			
		});
